﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using BLL.Validation;
using DAL;
using Entities;
using DAL.IRepository;
using System.Linq;


namespace WindowsApp.QLKeHoachGiangDay
{
    public partial class FrmQLKeHoachGiangDay : DevComponents.DotNetBar.Office2007Form
    {
        public FrmQLKeHoachGiangDay()
        {
            InitializeComponent();
            dgvDSKeHoach.AutoGenerateColumns = false;
        }
         private IGeneralRepository<QuanLyKeHoachGiangDay> _repository;
        private QuanLyKeHoachGiangDayManager _manager;
        QuanLyKeHoachGiangDay _quanlyKH;
        private bool _selectAll = false;


        private void FrmQLKeHoachGiangDay_Load(object sender, EventArgs e)
        {
            _repository = new GeneralRepository<QuanLyKeHoachGiangDay>();
            _manager = new QuanLyKeHoachGiangDayManager(_repository);
            dgvDSKeHoach.DataSource = _repository.GetAll().ToList();
            txtNamHoc.Focus();

        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            dgvDSKeHoach.DataSource = _repository.GetAll().ToList();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            var frm = new frmAddQuanLyKHGD(_manager, this);
            frm.Text = "Thêm kế hoạch";
            //if (frm.ShowDialog() == DialogResult.OK)
            frm.Show();
            //btnLamMoi.PerformClick();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa các kế hoạch đã chọn?", "Xóa kế hoạch", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
                return;
            foreach (DataGridViewRow row in dgvDSKeHoach.Rows)
            {
                if (row.Cells[0].Value != null && row.Cells[0].Value.Equals(true))
                    _manager.Delete(row.Cells[1].Value.ToString());
            }
            dgvDSKeHoach.DataSource = _repository.GetAll().ToList();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            var aResult = _manager.Find(s => s.NamHoc.IndexOf(txtNamHoc.Text) >= 0);
            if (aResult.Success)
                dgvDSKeHoach.DataSource = aResult.Result;
            else
            {
                MessageBox.Show(aResult.GetErrorMessages());
                btnLamMoi.PerformClick();
                txtNamHoc.ResetText();
                txtNamHoc.Focus();
            }
        }

        private void btnSuaXem_Click(object sender, EventArgs e)
        {
            if (dgvDSKeHoach.SelectedRows.Count > 0)
            {
                _quanlyKH = new QuanLyKeHoachGiangDay();
                DataGridViewRow row = dgvDSKeHoach.SelectedRows[0];
                _quanlyKH.MaQL = row.Cells[1].Value.ToString();
                _quanlyKH.NamHoc = row.Cells[2].Value.ToString();
                _quanlyKH.HocKi = row.Cells[3].Value.ToString();

                var frm = new frmAddQuanLyKHGD(_manager,this, _quanlyKH);
                frm.Text = "Cập nhật kế hoạch";
                //if (frm.ShowDialog() == DialogResult.OK)
                frm.Show();
                    dgvDSKeHoach.DataSource = _repository.GetAll().ToList();
            }
            else
                MessageBox.Show("Chưa chọn kế hoạch để sửa", "Thông báo");

        }

        private void dgvDSKeHoach_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 0 && dgvDSKeHoach.Rows.Count > 0)
            {
                _selectAll = !_selectAll;
                for (int idx = 0; idx < dgvDSKeHoach.Rows.Count; idx++)
                {
                    dgvDSKeHoach.Rows[idx].Selected = _selectAll;
                    dgvDSKeHoach.Rows[idx].Cells[0].Value = _selectAll;
                }
            }
        }

        private void dgvDSKeHoach_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && dgvDSKeHoach.Rows[e.RowIndex].Cells[0].Value != null)
            {
                bool selected = (bool)dgvDSKeHoach.Rows[e.RowIndex].Cells[0].Value;
                dgvDSKeHoach.Rows[e.RowIndex].Selected = selected;
            }
        }

        private void txtNamHoc_TextChanged(object sender, EventArgs e)
        {
            var aResult = _manager.Find(s => s.NamHoc.IndexOf(txtNamHoc.Text) >= 0);
            if (aResult.Success)
                dgvDSKeHoach.DataSource = aResult.Result;
            else
                dgvDSKeHoach.DataSource = null;
        }
    }
}
